<?php
class Mozg_Controller
{
	/**
	 * объекты контроллеров
	 */
	static private $_controllers = array();
	
	private function __construct (){}
	
    /**
		возвращает объект контроллера
     */
    static public function Get($moduleName, $ctrlName = false, $protName = false, $envName = false)
    {
		if (!isset(self::$_controllers[$moduleName][$ctrlName][$protName][$envName]))
		{
			self::$_controllers[$moduleName][$ctrlName][$protName][$envName] = self::CreateController($moduleName, $ctrlName, $protName, $envName);
		}
		
		return self::$_controllers[$moduleName][$ctrlName][$protName][$envName];
    }

    static protected function CreateController($moduleName, $ctrlName, $protName, $envName)
    {
    	if (false === $ctrlName) $ctrlName = 'Default';
    	
        $ctrlClassNameEnv = 'App_Module_' .$moduleName. '_Component_' .$ctrlName. '_Ctrl_' .$protName. '_' .$envName;
        if (!class_exists($ctrlClassNameEnv))
        {
        	$ctrlClassNameEnv = 'App_Module_' .$moduleName. '_Component_' .$ctrlName. '_Ctrl_' .$protName;
        	if (!class_exists($ctrlClassNameEnv))
        	{
        		$ctrlClassNameEnv = 'App_Module_' .$moduleName. '_Component_' .$ctrlName. '_Ctrl_Base';
        		if (!class_exists($ctrlClassNameEnv))
        		{
        			throw new Mozg_Exception_NotFound('Неизвестный контроллер ' . $ctrlName);
        		}
        	}
        }

        return new $ctrlClassNameEnv($protName, $envName);
    }
    
    static public function GetCtrlPath($moduleName, $ctrlName)
    {
        return Mozg_Module::GetPath($moduleName) . 'Component/' . ucwords($ctrlName). '/';
    }
}